package com.fcscs.cservice.client.common;

import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.google.gwt.event.dom.client.KeyCodes;

public class TextFieldAndIconButtonField extends AdapterField {

	private String style = ShareGWTConstants.CSS_SEARCH_ICON;
	private HorizontalPanel myhp;
	private TextField<String> textField;
	private HighLightIconButton hicButton ;
	private TextFieldListener textFieldListener = null;
	
	public interface TextFieldListener {
		  void onTextFieldEnterKeyPressSearch(String value);
		  void onIconButtoSearch(String value);
	}
	
	public TextFieldAndIconButtonField(TextFieldListener l) {
		super(new HorizontalPanel());
		this.textFieldListener = l;
		myhp = (HorizontalPanel) getWidget();
		myhp.setId("commonHorizontalPanel_id");
		
		textField = new TextField<String>();
		textField.setId("commonIcoTextField_id");
		textField.addListener(Events.OnKeyPress, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (be.getKeyCode() == KeyCodes.KEY_ENTER) {
					textFieldListener.onTextFieldEnterKeyPressSearch(textField.getValue());
				}
			}
		});
		myhp.add(textField);
		
		hicButton = new HighLightIconButton(style, new SelectionListener<IconButtonEvent>() {
			
			@Override
			public void componentSelected(IconButtonEvent ce) {
				textFieldListener.onIconButtoSearch(textField.getValue());
			}
		});
		hicButton.setId("commonHighIconButton_id");
		hicButton.setStyleAttribute("marginLeft", "15px");
		myhp.add(hicButton);
	}
	
	
	public HighLightIconButton getHicButton() {
		return hicButton;
	}


	public void setStyle(String style) {
		this.style = style;
	}
	
	public void setTextFieldTitle(String v){
		textField.setTitle(v);
	}
	
	public void setIcoButtonTitle(String v){
		hicButton.setTitle(v);
	}
	
	public void clearTextField(){
		textField.clear();
	}
	
	public String getTextFieldValue(){
		return textField.getValue();
	}
	
	public TextField<String> getTextField(){
		return textField;
	}
	
}
